package testing.module;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import utilities.module.Module;

public class DoodleWithImage extends Module
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7796905954380277883L;

	protected BufferedImage img;
	protected BufferedImage img2;
	
	protected boolean fadeIn = true;
	
	protected float fadeVal = 0.0f;
	protected long fadeTime = 10000;
	protected long changeFadeTime = 0;
	protected long startTime = 0;
	
	protected int BANNER_WIDTH = 600;
	protected int BANNER_HEIGHT = 131;
	
	protected int BANNER_X = 100;
	protected int BANNER_Y = 100;
	
	protected double SCALE = 1.;

	protected double scaleVal = 0.0;
	protected long scaleTime = 5000;
	
	protected AffineTransform trans = null;
	
	public DoodleWithImage()
	{
		try 
		{
			img = ImageIO.read(new File("resources//imgs//title//wingslogocropped.png"));
			img2 = ImageIO.read(new File("resources//imgs//title//buttons//CREDITS.png"));
		}
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		startTime = System.currentTimeMillis();
		changeFadeTime = startTime + (long)fadeTime;
		trans = new AffineTransform();
		trans.scale(SCALE, SCALE);
		trans.translate(BANNER_X / SCALE, BANNER_Y / SCALE);
	}

	public void drawMe(Graphics g) 
	{
		if(img != null)
		{
			Graphics2D g2 = (Graphics2D)g;
			
			g2.setColor(Color.black);
			
			g2.fillRect(0, 0, 300, 700);
			
			//g2.setTransform(trans);

			//g2.scale(SCALE, SCALE);
			//g2.scale(scaleVal, scaleVal);
			
			//if(fade) g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
			
		//	if(fadeIn)
			//	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fadeVal));
			//else
			//	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1 - fadeVal));

			//g2.drawImage(img, (int)(100 / scaleVal), (int)(100 / scaleVal), (int)(100 / scaleVal) + BANNER_WIDTH, (int)(100 / scaleVal) + BANNER_HEIGHT, 0, 0, BANNER_WIDTH, BANNER_HEIGHT, null);
			//g2.drawImage(img.getScaledInstance((SCALE), (SCALE), Image.SCALE_DEFAULT), (int)(100 / scaleVal), (int)(100 / scaleVal), (int)(100 / scaleVal) + BANNER_WIDTH, (int)(100 / scaleVal) + BANNER_HEIGHT, 0, 0, BANNER_WIDTH, BANNER_HEIGHT, null);
			//g2.drawImage(img, 100, 100, 100 + (int)(BANNER_WIDTH * SCALE), 100 + (int)(BANNER_HEIGHT * SCALE), 0, 0, (int)(BANNER_WIDTH * SCALE), (int)(BANNER_HEIGHT * SCALE), null);
			//g2.drawImage(img, 0 + BANNER_X, 0 + BANNER_Y, img.getWidth() + BANNER_X, img.getHeight() + BANNER_Y, 0, 0, img.getWidth(), img.getHeight(), null);
			//g2.drawImage(img2, 0, 0, img2.getWidth(), img2.getHeight(), 0, 0, img2.getWidth(), img2.getHeight(), null);
			//g2.drawImage(img, trans, null);
			
			g2.setColor(Color.red);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
			//g2.setComposite(AlphaComposite.SrcIn);
			//g2.drawImage(img2, BANNER_X, BANNER_Y, img2.getWidth(), img2.getHeight(), null);
			//g2.fillRect(BANNER_X, BANNER_Y, img2.getWidth(), img2.getHeight());
			g2.drawImage(img, trans, null);
		}
	}

	public void update(Properties globals) 
	{
		//fadeIn = !fadeIn;
		
		//fadeVal = ((System.currentTimeMillis() - startTime) % fadeTime) / (fadeTime * 1.0f);
		
		//if(changeFadeTime < System.currentTimeMillis())
		//{
		//	fadeIn = !fadeIn;
		//	changeFadeTime = System.currentTimeMillis() + fadeTime;
		//}
		
		fadeVal = (float)Math.abs(Math.sin(System.currentTimeMillis() * (2 * Math.PI / fadeTime)));
		scaleVal = Math.abs(Math.cos(System.currentTimeMillis() * (2 * Math.PI / scaleTime)));
		//scaleVal = 1.0;
		//trans.scale(1, 1);
		//trans.scale(scaleVal, scaleVal);
		
		//trans.scale((fadeIn ? 0.5 : 1), (fadeIn ? 0.5 : 1));
		
		//trans = new AffineTransform();
		//trans.scale(scaleVal, scaleVal);
	}
}